#include "calServer.hpp"
#include <memory>

using namespace std;
using namespace server;

void Usage(const string &proc)
{
    cout << "\nUsage:\n\t" << proc << " local_port\n"
         << endl;
}

bool cal(const request &req, response &resp)
{
    resp._exitcode = OK;
    resp._result = OK;
    resp._msg = "正常结果, nice!";
    switch (req._op)
    {
    case '+':
        resp._result = req._x + req._y;
        break;
    case '-':
        resp._result = req._x - req._y;
        break;
    case '*':
        resp._result = req._x * req._y;
        break;
    case '/':
    {
        if (req._y == 0)
        {
            resp._msg = "你都除零啦, 还要啥结果呢！";
            resp._exitcode = DIV_ZERO;
        }
        else
            resp._result = req._x + req._y;
    }
    break;
    case '%':
    {
        if (req._y == 0)
        {
            resp._msg = "你都模零啦，还要啥结果呢！";
            resp._exitcode = MOD_ZERO;
        }
        else
            resp._result = req._x % req._y;
    }
    break;
    default:
    {
        resp._exitcode = OP_ERROR;
        resp._msg = "操作符都出错了，长点心吧！好好检查一下";
    }
    break;
    }
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(USAGE_ERR);
    }

    uint16_t port = atoi(argv[1]);

    unique_ptr<TcpServer> tsvr(new TcpServer(port));

    tsvr->initServer();
    tsvr->run(cal);

    // test();

    return 0;
}